package com.ruoyi.chengji.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pingjiao.domain.Coureassess;
import com.ruoyi.pingjiao.service.CourseAssessService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("score/courseAssess")
public class CourseAssessController extends BaseController {
    @Autowired
    private CourseAssessService courseAssessService;

    /**
     * 查询课程目标列表
     * @param coureassess
     * @return
     */
    @PreAuthorize("@ss.hasPermi('score:courseassess:list')")
    @GetMapping("list")
    public TableDataInfo list(Coureassess coureassess){
        startPage();
        List<Coureassess>list = courseAssessService.selectCourseAssessList(coureassess);
        return getDataTable(list);
    }

    /**
     * 导出课程目标信息
     * @param response
     * @param coureassess
     */
    @PreAuthorize("@ss.hasPermi('score:courseassess:export')")
    @PostMapping("export")
    public void export(HttpServletResponse response,Coureassess coureassess){
        List<Coureassess> list = courseAssessService.selectCourseAssessList(coureassess);
        ExcelUtil<Coureassess>excelUtil=new ExcelUtil<>(Coureassess.class);
        excelUtil.exportExcel(response,list,"导出课程目标列表");
    }

    /**
     * 获取课程目标详细信息
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('score:courseassess:info')")
    @GetMapping("{id}")
    public AjaxResult getCourseAssessById(@PathVariable Long id){
        return AjaxResult.success(courseAssessService.getCourseAssessById(id));
    }

    /**
     * 添加课程目标信息
     * @param coureassess
     * @return
     */
    @PreAuthorize("@ss.hasPermi('score:courseassess:add')")
    @PostMapping("add")
    public AjaxResult add(@RequestBody Coureassess coureassess){
        return toAjax(courseAssessService.add(coureassess));
    }

    /**
     * 修改课程目标信息
     * @param coureassess
     * @return
     */
    @PreAuthorize("@ss.hasPermi('score:courseassess:update')")
    @PutMapping("update")
    public AjaxResult update(@RequestBody Coureassess coureassess){
        return toAjax(courseAssessService.update(coureassess));
    }

    /**
     * 根据id删除课程目标
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('score:courseassess:delete')")
    @DeleteMapping("delete/{id}")
    public AjaxResult delete(@PathVariable Long[] id){
        return toAjax(courseAssessService.delete(id));
    }

    /**
     * 下载模板
     * @param response
     */
    @PostMapping("importTemplate")
    public void importTemplate(HttpServletResponse response){
        ExcelUtil<Coureassess>coureassessExcelUtil=new ExcelUtil<>(Coureassess.class);
        coureassessExcelUtil.importTemplateExcel(response,"课程目标");
    }

    /**
     * 导入数据
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("importDate")
    public AjaxResult importDate(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Coureassess>excelUtil=new ExcelUtil<>(Coureassess.class);
        List<Coureassess> list = excelUtil.importExcel(file.getInputStream());
        String username = getUsername();
        String mssage= courseAssessService.importDate(list,updateSupport,username);
        return success(mssage);
    }

    /**
     * 通过课程id查询各课程目标的平均达成度
     * @param courseId
     * @return
     */
    @GetMapping("getAverage/{courseId}")
    public AjaxResult getAverage(@PathVariable("courseId") Integer courseId){
        return AjaxResult.success(courseAssessService.getAverageByCourseId(courseId));
    }

    /**
     * 通过课程id查询各课程目标的达成度成绩
     * @param courseId
     * @return
     */
    @GetMapping("getScore/{courseId}")
    public AjaxResult getScore(@PathVariable Integer courseId){
        return AjaxResult.success(courseAssessService.getScoreByCourseId(courseId));
    }
//    @GetMapping("getExcepted/{courseId}")
//    public AjaxResult getExcepted(@PathVariable Integer courseId){
//
//    }
}
